package com.nerve.android.cloudoffice.app.activity.task;

import java.util.HashMap;
import java.util.Map;

import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;
import org.nerve.android.ui.dialog.ConfirmDialog;
import org.nerve.android.ui.dialog.DialogHelper.ConfirmListener;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.domain.Daily;
import com.nerve.android.cloudoffice.service.NetService;

@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_daily_add)
public class DailyActivity extends NavigationActivity implements OnClickListener, ConfirmListener{
	
	private static final int DEL_OK = 1001;
	
	public static final String DAILY = "DAILY";
	
	@ViewOnId(id=R.id.name)
	private EditText nameET;
	@ViewOnId(id=R.id.remark)
	private EditText remarkET;
	@ViewOnId(id=R.id.okBtn, clickListener="this")
	public Button okBtn;
	
	private Daily daily;
	
	private ConfirmDialog delDialog;
	
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			hideProgressDialog();
			switch (msg.what) {
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			//保存成功
			case WORKER_OK:
				toast("保存成功");
				return ;
			case DEL_OK:
				toast("删除成功");
				onNavigateBackButtonClick();
				break;
			}
		}
	};
	
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.okBtn){
			if(delDialog == null){
				delDialog = new ConfirmDialog(this, "删除每日任务", "你确定要删除这个每日任务吗？");
				delDialog.setConfirmListener(this);
			}
			delDialog.show();
		}
	}
	
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("查看每日任务");
		customNavigateRightButton("保存", true);
		
		if(daily == null){
			okBtn.setVisibility(View.GONE);
			daily = new Daily();
			daily.setId(0);
		}else{
			nameET.setText(daily.getTitle());
			remarkET.setText(daily.getRemark());
		}
	}
	
	@Override
	protected void initData() {
		Object obj = getData(DAILY, true);
		if(obj != null)
			daily = (Daily)obj;
	}
	
	@Override
	protected void onNavigateBackButtonClick() {
		finish();
	}
	
	@Override
	protected void onNavigateRightButtonClick() {
		if(isEmpty(nameET)){
			toast("标题不能为空！");
			return ;
		}
		
		daily.setTitle(nameET.getText().toString());
		daily.setRemark(getText(remarkET));
		
		new DailyWorker().execute(WORKER_OK);
	}
	

	@Override
	public void onConfirmClick(int position, Object obj) {
		if(position == ConfirmDialog.OK){
			new DailyWorker().execute(DEL_OK);
		}
	}
	
	private class DailyWorker extends AsyncTask<Integer, Void, Integer>{

		@Override
		protected Integer doInBackground(Integer... params) {
			Reply re = new Reply();
			
			String url = null;
			if(params[0] == DEL_OK){
				url = Const.HOST + "task/dailyDel.htm?id=" + daily.getId();
				re = NetService.get(url);
			}
			else{
				url = Const.HOST + "task/dailyAdd.htm";
				Const.log("URL::::::::::::::; " + url);
				Map<String, String> map = new HashMap<String, String>();
				map.put("daily.title", daily.getTitle());
				map.put("daily.remark", daily.getRemark());
				map.put("daily.id", daily.getId()+"");
				
				re = NetService.post(url, map);
			}
			
			if(re.isError()){
				Const.ERROR = re.getMessage().toString();
				return WORKER_FAIL;
			}
				
			return params[0];
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}
	}

}